$(document).ready(function() {
	set_footer_position();
	img_button_bind("#submit_button", "imgs/register/submit_button_normal.png", "imgs/register/submit_button_hover.png", "imgs/register/submit_button_active.png");
	img_button_bind("#has_login_button", "imgs/register/login_button_normal.png", "imgs/register/login_button_hover.png", "imgs/register/login_button_active.png");
	img_button_bind("#login_button", "imgs/common/login_button_normal.jpg", "imgs/common/login_button_hover.jpg", "imgs/common/login_button_active.jpg");
	img_button_bind("#quit_button", "imgs/common/quit_button_normal.jpg", "imgs/common/quit_button_hover.jpg", "imgs/common/quit_button_active.jpg");
	$("#submit_button").click(judge_register);
	$("#has_login_button").click(show_login_panel);
	$("#login_button").click(judge_login);
	$("#quit_button").click(quit_login_panel);
	forget_password();
	login_table_position();
	judge_username_bind();
	judge_blog_name_bind();
});

/**
* 异步判断输入的邮箱是否被占用
*/
function judge_username_bind() {
	$("#e_mail_text").blur(function() {
		var username = $(this).val();
		if(username == "") return;
		var request = createAjaxRequest();
		request.open("POST", "server/test_username.php", true);
		request.onreadystatechange = function() {
			if(request.readyState == 4 && request.status == 200) {
				var is_conflict = request.responseText;
				if(is_conflict == "1") {
					$("#e_mail_info").text("邮箱已被占用");
				} else {
					$("#e_mail_info").text("");
				}
			}
		}
		request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		request.send("username=" + username);
	});
}

/**
* 异步判断输入的博客名是否被占用
*/
function judge_blog_name_bind() {
	$("#blog_name_text").blur(function() {
		var blog_name = $(this).val();
		if(blog_name == "") return;
		var request = createAjaxRequest();
		request.open("POST", "server/test_blog_name.php", true);
		request.onreadystatechange = function() {
			if(request.readyState == 4 && request.status == 200) {
				var is_conflict = request.responseText;
				if(is_conflict == "1") {
					$("#blog_name_info").text("博客名已被占用");
				} else {
					$("#blog_name_info").text("");
				}
			}
		}
		request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		request.send("blog_name=" + blog_name);
	});
}

/**
 * 判断注册时的客户输入的内容是否正确
 */
function judge_register() {
	var account = $("#e_mail_text").val();
	if (account == "") {
		$("#e_mail_info").text("请输入邮箱");
		return;
	}

	var pattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //判断邮箱格式的正则表达式
	var ismatch = pattern.test(account);
	if(!ismatch) {
		$("#e_mail_info").text("请输入正确的邮箱格式");
		return;
	} else {
		$("#e_mail_info").text("");
	}

	var password = $("#password_text").val();
	if(password == "") {
		$("#password_info").text("请输入密码");
		return;
	} else if(password.length < 6) {
		$("#password_info").text("密码必须不小于6位");
		return;
	} else {
		$("#password_info").text("");
	}

	var confirm_password = $("#confirm_password_text").val();
	if(confirm_password != password) {
		$("#confirm_password_info").text("两次输入密码不相同");
		return;
	} else {
		$("#confirm_password_info").text("");
	}

	var question = $("#question_text").val();
	if(question == "") {
		$("#question_info").text("请输入密码提示问题");
		return;
	} else {
		$("#question_info").text("");
	}

	var answer = $("#answer_text").val();
	if(answer == "") {
		$("#answer_info").text("请输入密码问题答案");
		return;
	} else {
		$("#answer_info").text("");
	}

	var blog_name = $("#blog_name_text").val();
	if(blog_name == "") {
		$("#blog_name_info").text("请输入博客名称");
		return;
	} else {
		$("#blog_name_info").text("");
	}

	var request = createAjaxRequest();
	request.open("POST", "server/test_username.php", false);
	var username = $("#e_mail_text").val();
	request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	request.send("username=" + username);
	var is_conflict = request.responseText;
	if(is_conflict == "1") {
		$("#e_mail_info").text("邮箱已被占用");
		return;
	}


	request = createAjaxRequest();
	request.open("POST", "server/test_blog_name.php", false);
	var blog_name = $("#blog_name_text").val();
	request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	request.send("blog_name=" + blog_name);
	var is_conflict = request.responseText;
	if(is_conflict == "1") {
		$("#blog_name_info").text("博客名已被占用");
		return;
	}

	$("form").first().submit();
}

